package cn.smart.service.impl;

import cn.smart.mapper.FgroupMapper;
import cn.smart.mapper.GardenMapper;
import cn.smart.pojo.Fgroup;
import cn.smart.pojo.FgroupExample;
import cn.smart.pojo.Garden;
import cn.smart.service.FgroupService;
import cn.smart.util.ExceptionUtil;
import cn.smart.util.Info;
import cn.smart.util.SmartResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Xiezh
 * @Description:
 * @create 2017/8/16
 */
@Service
public class FgroupServiceImpl implements FgroupService {

    @Value("${FGROUP_NICKNAME}")
    private String FGnickName;//花组的昵称
    @Value("${FGROUP_TYPE}")
    private int FGtype;//花组的状态
    @Value("${FGROUP_SOIL_DRYNESS}")
    private int FGsoilDryness;//花组的土壤干度阈值
    @Value("${FGROUP_AIR_HUMIDITY}")
    private int FGairHumidity;//花组的空气湿度阈值
    @Value("${FGROUP_AIR_TEMPERATURE}")
    private int FGairTemperature;//花组的空气温度阈值
    @Value("${FGROUP_DROP_TIME_CELL}")
    private int FGdropTimeCell;//花组的滴灌时间间隔 单位分钟
    @Value("${FGROUP_SPRAY_TIME_CELL}")
    private int FGsparyTimeCell;//花组的定时喷雾时间间隔，单位分钟
    @Value("${FGROUP_DROP_TIME_SPAN}")
    private int FGdropTimeSpan;//花组每次滴灌的时长 单位秒
    @Value("${FGROUP_SPARY_TIME_SPAN}")
    private int FGsparyTimeSpan;//花组每次喷雾的时长，单位秒
    @Value("${FGROUP_MODE}")
    private int FGmode;//花组的模式 1=智能模式 0=普通模式

    @Autowired
    private FgroupMapper fgroupMapper;
    @Autowired
    private GardenMapper gardenMapper;

    /**
     * 设置花组的一些默认参数
     * @param fgroup
     * @return
     */
    private Fgroup checkEmpty(Fgroup fgroup){
        //花组的昵称
        if (fgroup.getFgnickname() == null) fgroup.setFgnickname(FGnickName);
        //花组的种类
        if (fgroup.getFgtype() == null) fgroup.setFgtype(FGtype);
        //花组的土壤干度阈值
        if (fgroup.getFgsoildryness() == null) fgroup.setFgsoildryness(FGsoilDryness);

        //花组的空气温度阈值
        if (fgroup.getFgairtemperature() == null) fgroup.setFgairtemperature(FGairTemperature);
        //花组的空气湿度阈值
        if (fgroup.getFgairhumidity() == null) fgroup.setFgairhumidity(FGairHumidity);
        //花组的定时滴灌时间间隔
        if (fgroup.getFgdroptimecell() == null) fgroup.setFgdroptimecell(FGdropTimeCell);
        //花组的定时喷雾时间间隔
        if (fgroup.getFgspraytimecell() == null) fgroup.setFgspraytimecell(FGsparyTimeCell);
        //花组的滴灌时长
        if (fgroup.getFgdroptimespan() == null) fgroup.setFgdroptimespan(FGdropTimeSpan);
        //花组的喷雾时长
        if (fgroup.getFgspraytimespan() == null) fgroup.setFgspraytimespan(FGsparyTimeSpan);
        //花组的模式
        if (fgroup.getFgmode() == null) fgroup.setFgmode(FGmode);
        return fgroup;
    }
    private SmartResult getFailResult(Exception e){
        return new SmartResult(500,Info.FAIL,ExceptionUtil.getStackTrace(e));
    }
    private SmartResult getFailResult(String line){
        return new SmartResult(200,Info.FAIL,line);
    }
    private SmartResult getSuccessResult(Object object){
        return  new SmartResult(200,Info.SUCCESS,object);
    }
    /**
     * 新增一个花组
     *
     * @param fgroup
     * @return
     */
    @Override
    public SmartResult insertFgroup(Fgroup fgroup) {
        FgroupExample example = new FgroupExample();
        FgroupExample.Criteria criteria = example.createCriteria();
        //检测必要的参数是否已经设置
        if (fgroup.getFggid() == null || fgroup.getFgnumber() == null) {
            //如果花组的 编号 或者，所属的花园主键为空
            return new SmartResult(200, Info.FAIL, "参数不全");
        }
        //判断要新增的花组 编号 是否已经被使用
        criteria.andFgnumberEqualTo(fgroup.getFgnumber());
        List<Fgroup> fgroupList = fgroupMapper.selectByExample(example);
        if (fgroupList.size() > 0) {
            //所用的编号已经被使用
            return new SmartResult(200, Info.FAIL, "编号已被使用");
        }
        //判断所用的编号是否超出，花园最大花组数，或者小于0
        Garden garden = gardenMapper.selectByPrimaryKey(fgroup.getFggid());
        if (garden.getGmax() < fgroup.getFgnumber() || fgroup.getFgnumber() < 0) {
            return new SmartResult(200, Info.FAIL, "编号超过范围");
        }
        fgroup = checkEmpty(fgroup);
        //设置花组创建的时间
        fgroup.setFgtime(new Date());
        //新增花组
        try{
            //插入成功
            fgroupMapper.insert(fgroup);
            return getSuccessResult(null);

        }catch (Exception e){
            //插入失败
            e.printStackTrace();
            return getFailResult(e);
        }
    }

    /**
     * 删除一个花组
     * @param fgId
     * @return
     */
    @Override
    public SmartResult deleteFgroup(int fgId) {

        //判断要删除的花组是否存在
        Fgroup fgroup = fgroupMapper.selectByPrimaryKey(fgId);
        if(fgroup==null){
            //花组不存在
            return new SmartResult(200,Info.FAIL,"花组不存在");
        }
        //删除花组
        try{
            fgroupMapper.deleteByPrimaryKey(fgId);
            return getSuccessResult(null);
        }catch (Exception e){
            e.printStackTrace();
            return getFailResult(e);
        }
    }

    /**
     * 更新花组
     * @param fgroup
     * @return
     */
    @Override
    public SmartResult updateFgroup(Fgroup fgroup) {
        //保证不能修改的部分不被修改
        //得到旧的花组
        Fgroup oldFgroup = fgroupMapper.selectByPrimaryKey(fgroup.getFgid());
        if(oldFgroup==null){
            return  getFailResult("被更新的花组不存在");
        }

        try{
            fgroupMapper.updateByPrimaryKey(fgroup);
            return  getSuccessResult(null);
        }catch (Exception e){
            e.printStackTrace();
            return  getFailResult(e);
        }
    }

    /**
     * 通过主键查询花组
     * @param fgid
     * @return
     */
    @Override
    public SmartResult getFgroupByFgid(int fgid) {
        Fgroup fgroup = fgroupMapper.selectByPrimaryKey(fgid);
        return getSuccessResult(fgroup);
    }

    /**
     * 查询用户名下的所有花组
     * @param fgGid
     * @return
     */
    @Override
    public SmartResult getUserAllFgroup(int fgGid) {
        FgroupExample example = new FgroupExample();
        FgroupExample.Criteria criteria = example.createCriteria();
        criteria.andFggidEqualTo(fgGid);
        List<Fgroup> fgroups = fgroupMapper.selectByExample(example);
        return getSuccessResult(fgroups);
    }
}
